﻿using SmartPlant.Model;
using SmartPlant.Model.Biz;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Http;
using System.Security.Cryptography;
using System.IO;
using System.Threading.Tasks;
using System.Web;
using static System.Formats.Asn1.AsnWriter;
using SmartPlant.Model.DbContext;
using Org.BouncyCastle.Asn1.Ocsp;

namespace SmartPlant.Services
{
    public class InstrumentService : BaseService, IInstrumentService, ISpecSheetDataService
    {

        public async Task<List<Instruments>> GetInstrumentsAsync(decimal proId, string sqlWhere = "*", string select = "cmpnt_id,cmpnt_name", string orderby = "cmpnt_seq,cmpnt_name")
        {
            string uri = $"/api/Project/{proId}/components-api?sqlwhere={sqlWhere}&select={select}&orderby={orderby}";
            var res = await GetAsync<List<Instruments>>(uri);//虽然后端是List<dynamic>返回的，但是基于的是Instrument的仓储，所以可以是Instruments
            return res.data;
        }

        /// <summary>
        /// 右键-查看仪表属性
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="insId"></param>
        /// <returns></returns>
        public async Task<Instruments> GetInstrumentAsync(decimal proId, string insId)
        {
            string uri = $"/api/Project/{proId}/components-api/{insId}";
            var res = await GetAsync<Instruments>(uri);
            return res.data;
        }


        /// <summary>
        /// 创建仪表
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="unitid"></param>
        /// <param name="loopid"></param>
        /// <param name="typeid"></param>
        /// <param name="cmpntname"></param>
        /// <param name="copyId"></param>
        /// <returns></returns>
        public async Task<RESTfulResult<string>> CreateInstrumentAsync(decimal proId, decimal unitid, decimal loopid, decimal typeid, string cmpntname, decimal copyId = 0)
        {
            string uri = $"/api/Project/{proId}/components-api/{unitid}/{loopid}/{typeid}/{Uri.EscapeDataString(cmpntname)}/{copyId}";
            var r = await PostAsync<string>(uri);//正常返回新的id
            return r;
        }

        public async Task<bool> UpdateInstrumentAsync(decimal proId, Instruments insItem)
        {
            string uri = $"/api/Project/{proId}/components-api";
            var r = await PutBodyAsync<bool, Instruments>(uri, insItem);
            return r.data;
        }

        public async Task<bool> DeleteInstrumentAsync(decimal proId, string objid)
        {
            string url = $"/api/Project/{proId}/components-api/{objid}";
            var rr = await DeleteAsync<bool>(url);
            return rr.succeeded;
        }

        public async Task<List<ComponentFunctionType>> GetComponentFunctionTypes(decimal proId, string sqlwhere = "CMPNT_FUNC_TYPE_ID > 0", string select = "CMPNT_FUNC_TYPE_ID,CMPNT_FUNC_TYPE_NAME,CMPNT_FUNC_TYPE_DESC,PROC_FUNC_ID", string orderby = "PROC_FUNC_ID,CMPNT_FUNC_TYPE_NAME")
        {
            string url = $"/api/Project/{proId}/component-function-type-api?sqlwhere={sqlwhere}&select={select}&orderby={orderby}";
            var res = await GetAsync<List<ComponentFunctionType>>(url);
            return res.data;
        }
        #region specSheetData
        /// <summary>
        /// 查询仪表的规格书，把excel模板填充数据后，下载给前端
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="cmpntId"></param>
        /// <returns></returns>
        public async Task<RESTfulResult<FileContentResultBaseString>> DownSpecOutputAsync(string proId, string templatePath, string cmpntId)
        {
            var postObj = new
            {
                cmpntId
            };
            string uri = $"/api/Project/{proId}/Spec-sheet-data-api/spec-output/{templatePath}/{cmpntId}";
            var result = await GetAsync<FileContentResultBaseString>(uri);
            return result;



        }

        /// <summary>
        /// 更新仪表的规格书。把excel带回去，和执行规格书导入有些类似
        /// ImportUtilityController/Index
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="reportname"></param>
        /// <param name="path"></param>
        /// <param name="unitid"></param>
        /// <param name="files"></param>
        /// <param name="blankValueUpdteFlag"></param>
        /// <returns></returns>
        public async Task<RESTfulResult<FileContentResultBaseString>> UploadSpecInputAsync(List<NetworkFileInfo> files, string projId, string templatePath, string cmpntId, bool blankValueUpdteFlag)
        {
            string url = $"/api/Project/{projId}/Spec-sheet-data-api/Spec-Input/{templatePath}/{cmpntId}/{blankValueUpdteFlag}";
            //string url = $"/api/Project/{proId}/import-utility-api/Spec/{reportname}/{path}/{unitid}";
            var b = await PostFileAsync<FileContentResultBaseString>(url, files);
            return b;

        }


        #endregion       
        /// <summary>
        /// 更新UDF
        /// </summary>
        /// <param name="proId"></param>
        /// <param name="udf"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> UpdateUDFAsync(decimal proId, UdfComponent udf)
        {
            string uri = $"/api/Project/{proId}/udf-components-api";
            RESTfulResult<bool> r = await PutBodyAsync<bool, UdfComponent>(uri, udf);

            return r.data;
        }

        public async Task<SpecSheetData> GetById(decimal cmpntId, decimal caseId = 0)
        {
            string uri = $"/api/Project/{GlobalObject.CurrentProject.ProjId}/spec-sheet-data-api/{cmpntId}/{caseId}";
            RESTfulResult<SpecSheetData> r = await GetAsync<SpecSheetData>(uri);

            return r.data;
        }
    }
}